package com.goodbyegirl1974.cardGameProto.model
{
	import com.goodbyegirl1974.cardGameProto.config.GameConfig;
	import com.goodbyegirl1974.cardGameProto.manager.BattleManager;
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class MainModel extends EventDispatcher
	{
		private var _battleManager:BattleManager;
		
		private var _player0DataXML:XML;
		private var _player1DataXML:XML;
		
		public function MainModel(target:IEventDispatcher=null)
		{
			super(target);
			init();
		}
		
		/**
		 * 
		 * @return 
		 * 
		 */		
		public function get battleManager():BattleManager
		{
			return _battleManager;
		}
		
		/**
		 * 初期化。
		 * 
		 */		
		private function init():void
		{
			this.loadXMLDataOfPlayer0();
		}
		
		/**
		 * 1人目のプレーヤーのパラメータXMLをロード。
		 * 
		 */		
		private function loadXMLDataOfPlayer0():void
		{
			trace("loadXMLDataOfPlayer0");
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, this.loadXMLDataOfPlayer0CompleteHandler);
			loader.load(new URLRequest("../xml/player00.xml"));
		}
		
		/**
		 * 一人目のプレーヤーのパラメータXMLのロード完了時のファンクション。
		 * @param event
		 * 
		 */		
		private function loadXMLDataOfPlayer0CompleteHandler(event:Event):void
		{
			var loader:URLLoader = event.currentTarget as URLLoader;
			loader.removeEventListener(Event.COMPLETE, this.loadXMLDataOfPlayer0CompleteHandler);
			this._player0DataXML = new XML(loader.data);
			this.loadXMLDataOfPlayer1();
			trace(_player0DataXML);
		}
		
		/**
		 * 2人目のプレーヤーのパラメータXMLをロード。
		 * 
		 */	
		private function loadXMLDataOfPlayer1():void
		{
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, this.loadXMLDataOfPlayer1CompleteHandler);
			loader.load(new URLRequest("../xml/player01.xml"));
		}
		
		/**
		 * 2人目のプレーヤーのパラメータXMLのロード完了時のファンクション。
		 * @param event
		 * 
		 */	
		private function loadXMLDataOfPlayer1CompleteHandler(event:Event):void
		{
			var loader:URLLoader = event.currentTarget as URLLoader;
			loader.removeEventListener(Event.COMPLETE, this.loadXMLDataOfPlayer1CompleteHandler);
			this._player1DataXML = new XML(loader.data);
			trace(this._player1DataXML);
			this.setBattleManager();
			//両プレーヤーのデータのロードが完了したので、カード表示に移行。
			this.dispatchEvent(new Event(GameConfig.LOAD_PLAYER_DATA_COMPLETE));
		}
		
		/**
		 * BattleManagerの生成。
		 * 
		 */		
		private function setBattleManager():void
		{
			this._battleManager = new BattleManager(this._player0DataXML, this._player1DataXML);
		}
	}
}